package com.alipay.app.groupmeal.standard.offline;

import com.alipay.app.groupmeal.standard.AppContext;
import com.alipay.app.groupmeal.standard.offline.bean.DaoMaster;
import com.alipay.app.groupmeal.standard.offline.bean.DaoSession;

import org.greenrobot.greendao.database.Database;

public class DbService {

    private static final String DB_NAME = "elementc-db";
    private static DaoMaster sDaoMaster;
    private static DaoSession sDaoSession;

    /**
     * 取得DaoMaster
     *
     * @return
     */
    private static DaoMaster getDaoMaster() {
        if (sDaoMaster == null) {
            MySQLiteOpenHelper helper = new MySQLiteOpenHelper(AppContext.context, DB_NAME);
            Database db = helper.getWritableDb();
            sDaoMaster = new DaoMaster(db);
        }
        return sDaoMaster;
    }

    /**
     * 取得DaoSession
     *
     * @return
     */
    public static DaoSession getDaoSession() {
        if (sDaoSession == null) {
            if (sDaoMaster == null) {
                sDaoMaster = getDaoMaster();
            }
            sDaoSession = sDaoMaster.newSession();
        }
        return sDaoSession;
    }

}
